home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / General / SpriteWorld 1.0b3 / Examples / SimpleBreakOut / SimpleBreakOut.c < prev    next >
Text File  |  1993-06-20  |  10KB  |  417 lines

  1. ///--------------------------------------------------------------------------------------
  2. // SimpleBreakOut.c
  3. //
  4. // By: Tony Myles
  5. //
  6. // Copyright © 1993 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __DESK__
  11. #include <Desk.h>
  12. #endif
  13.  
  14. #ifndef __EVENTS__
  15. #include <Events.h>
  16. #endif
  17.  
  18. #ifndef __OSEVENTS__
  19. #include <OSEvents.h>
  20. #endif
  21.  
  22. #ifndef __TOOLUTILS__
  23. #include <ToolUtils.h>
  24. #endif
  25.  
  26. #ifndef __SEGLOAD__
  27. #include <SegLoad.h>
  28. #endif
  29.  
  30. #ifndef __WINDOWS__
  31. #include <Windows.h>
  32. #endif
  33.  
  34. #ifndef __SPRITEWORLD__
  35. #include <SpriteWorld.h>
  36. #endif
  37.  
  38. #ifndef __SPRITELAYER__
  39. #include <SpriteLayer.h>
  40. #endif
  41.  
  42. #ifndef __SPRITE__
  43. #include <Sprite.h>
  44. #endif
  45.  
  46. #ifndef __FRAME__
  47. #include <Frame.h>
  48. #endif
  49.  
  50. #ifndef __SPRITEWORLDUTILS__
  51. #include <SpriteWorldUtils.h>
  52. #endif
  53.  
  54. #ifndef __APPLICATION__
  55. #include "Application.h"
  56. #endif
  57.  
  58. #ifndef __SIMPLEBREAKOUT__
  59. #include "SimpleBreakOut.h"
  60. #endif
  61.  
  62.  
  63. ///--------------------------------------------------------------------------------------
  64. // PerformSimpleAnimation
  65. ///--------------------------------------------------------------------------------------
  66.  
  67. void PerformSimpleAnimation(CWindowPtr srcWindowP)
  68. {
  69.     OSErr err;
  70.     PixPatHandle pixPatH;
  71.     SpriteWorldPtr spriteWorldP;
  72.     SpriteLayerPtr brickLayerP;
  73.     SpriteLayerPtr ballLayerP;
  74.     SpriteLayerPtr paddleLayerP;
  75.     SpritePtr brickSpriteArray[kTotalNumberOfBricks];
  76.     SpritePtr ballSpriteP;
  77.     SpritePtr paddleSpriteP;
  78.     long brickNum, ticks, frames;
  79.     short horizOffset, vertOffset, brickHoriz, brickVert, col, row;
  80.     Rect moveBoundsRect, worldRect;
  81.  
  82.     SetPort((GrafPtr)srcWindowP);
  83.     SetCursor(*GetCursor(watchCursor));
  84.  
  85.  
  86.     //
  87.     // STEP #1: initialize the sprite world package
  88.     //
  89.  
  90.     err = SWEnterSpriteWorld();
  91.     FatalError(err);
  92.  
  93.  
  94.     //
  95.     // STEP #2: create the various pieces that we need
  96.     //
  97.  
  98.         // create the sprite world
  99.     err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, NULL);
  100.     FatalError(err);
  101.  
  102.         // create the brick layer
  103.     err = SWCreateSpriteLayer(&brickLayerP);
  104.     FatalError(err);
  105.  
  106.         // create the ball layer
  107.     err = SWCreateSpriteLayer(&ballLayerP);
  108.     FatalError(err);
  109.  
  110.         // create the paddle layer
  111.     err = SWCreateSpriteLayer(&paddleLayerP);
  112.     FatalError(err);
  113.  
  114.     err = SWCreateSpriteFromCIconResource(brickSpriteArray, NULL, kBrickCIconID, 1, kFatMask);
  115.  
  116.         // create brick sprites
  117.     for (brickNum = 1; brickNum < kTotalNumberOfBricks; brickNum++)
  118.     {
  119.         err = SWCloneSprite(brickSpriteArray[0], brickSpriteArray + brickNum, NULL);
  120.         FatalError(err);
  121.     }
  122.  
  123.         // create ball sprite
  124.     err = SWCreateSpriteFromCIconResource(&ballSpriteP, NULL, kBreakBallCIconID, 1, kFatMask);
  125.     FatalError(err);
  126.  
  127.         // create paddle sprite
  128.     err = SWCreateSpriteFromCIconResource(&paddleSpriteP, NULL, kPaddleCIconID, 1, kFatMask);
  129.     FatalError(err);
  130.  
  131.  
  132.     //
  133.     // STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
  134.     //
  135.  
  136.     for (brickNum = 0; brickNum < kTotalNumberOfBricks; brickNum++)
  137.     {
  138.             // add the brick sprite to the brick layer
  139.         SWAddSprite(brickLayerP, brickSpriteArray[brickNum]);
  140.     }
  141.  
  142.         // add the ball sprite to the ball layer
  143.     SWAddSprite(ballLayerP, ballSpriteP);
  144.  
  145.         // add the paddle sprite to the paddle layer
  146.     SWAddSprite(paddleLayerP, paddleSpriteP);
  147.  
  148.         // add the layers to the world
  149.     SWAddSpriteLayer(spriteWorldP, brickLayerP);
  150.     SWAddSpriteLayer(spriteWorldP, ballLayerP);
  151.     SWAddSpriteLayer(spriteWorldP, paddleLayerP);
  152.  
  153.  
  154.     //
  155.     // STEP #4: lock the sprite world        !!! VERY IMPORTANT !!!
  156.     //
  157.  
  158.     SWLockSpriteWorld(spriteWorldP);
  159.  
  160.  
  161.     //
  162.     // STEP #5: set things up for the animation (must be done AFTER the sprite world is locked!)
  163.     //
  164.  
  165.         // calculate the movement boundary rectangle
  166.     moveBoundsRect = srcWindowP->portRect;
  167.  
  168.     horizOffset = (brickSpriteArray[0]->curFrameP->frameRect.right - brickSpriteArray[0]->curFrameP->frameRect.left) + 2;
  169.     vertOffset = (brickSpriteArray[0]->curFrameP->frameRect.bottom - brickSpriteArray[0]->curFrameP->frameRect.top) + 2;
  170.     brickHoriz = 2;
  171.     brickVert = (vertOffset * 2);
  172.     brickNum = 0;
  173.  
  174.         // set up the sprites
  175.     for (col = 0; col < kNumberOfBrickColumns; col++)
  176.     {
  177.         brickVert = (vertOffset * 2);
  178.  
  179.         for (row = 0; row < kNumberOfBrickRows; row++)
  180.         {
  181.                 // set the sprite’s initial location
  182.             SWSetSpriteLocation(brickSpriteArray[brickNum], brickHoriz, brickVert);
  183.             SWSetSpriteMoveTime(brickSpriteArray[brickNum], -1);
  184.  
  185.             brickNum++;
  186.             brickVert += vertOffset;
  187.         }
  188.     
  189.         brickHoriz += horizOffset;
  190.     }
  191.  
  192.         // set the ball’s movement characteristics
  193.     SWSetSpriteLocation(ballSpriteP, moveBoundsRect.left, moveBoundsRect.bottom - 100);
  194.     SWSetSpriteMoveBounds(ballSpriteP, &moveBoundsRect);
  195.     SWSetSpriteMoveDelta(ballSpriteP, 2, 2);
  196.     SWSetSpriteMoveTime(ballSpriteP, 6);
  197.     SWSetSpriteMoveProc(ballSpriteP, BallMoveProc);
  198.     SWSetSpriteCollideProc(ballSpriteP, BallCollideProc);
  199.  
  200.         // set the paddle’s movement characteristics
  201.     SWSetSpriteLocation(paddleSpriteP, moveBoundsRect.left, moveBoundsRect.bottom - 30);
  202.     SWSetSpriteMoveProc(paddleSpriteP, PaddleMoveProc);
  203.     
  204.     moveBoundsRect.right -= 32;
  205.     SWSetSpriteMoveBounds(paddleSpriteP, &moveBoundsRect);
  206.     SWSetSpriteCollideProc(paddleSpriteP, PaddleCollideProc);
  207.  
  208.  
  209.     SWSetPortToBackGround(spriteWorldP);
  210.     worldRect = srcWindowP->portRect;
  211.  
  212.     if (SWHasColorQuickDraw() && ((**srcWindowP->portPixMap).pixelSize > 1))
  213.     {
  214.             // fill the sprite world with a pretty pattern
  215.         pixPatH = GetPixPat(kBreakOutBackDropPixPatID);
  216.  
  217.         if (pixPatH != NULL)
  218.         {
  219.             FillCRect(&worldRect, pixPatH);
  220.             DisposePixPat(pixPatH);
  221.         }
  222.         else
  223.         {
  224.             FillRect(&worldRect, qd.ltGray);
  225.         }
  226.     }
  227.     else
  228.     {
  229.         FillRect(&worldRect, qd.ltGray);
  230.     }
  231.  
  232.     SetPort((GrafPtr)srcWindowP);
  233.  
  234.  
  235.     //
  236.     // STEP #6: run the animation
  237.     //
  238.  
  239.     HideCursor();
  240.  
  241.     SWUpdateSpriteWorld(spriteWorldP);
  242.  
  243.     while (!Button())
  244.     {
  245.         SWProcessSpriteWorld(spriteWorldP);
  246.  
  247.             // did we lose the last ball?
  248.         if (ballSpriteP->userData == 4)
  249.         {
  250.             SysBeep(1);
  251.             break;
  252.         }
  253.  
  254.             // see if the ball has hit the paddle
  255.         SWCollideSpriteLayer(paddleLayerP, ballLayerP);
  256.  
  257.             // see if the ball has hit the bricks
  258.         SWCollideSpriteLayer(ballLayerP, brickLayerP);
  259.  
  260.         SWAnimateSpriteWorld(spriteWorldP);
  261.  
  262.         SystemTask();
  263.     }
  264.  
  265.  
  266.     //
  267.     // STEP #7: unlock the sprite world
  268.     //
  269.  
  270.     SWUnlockSpriteWorld(spriteWorldP);
  271.  
  272.  
  273.     //
  274.     // STEP #8: dispose of the pieces we created
  275.     //
  276.  
  277.     for (brickNum = 0; brickNum < kTotalNumberOfBricks; brickNum++)
  278.     {
  279.         SWDisposeSprite(brickSpriteArray[brickNum], brickNum == 0);
  280.     }
  281.  
  282.     SWDisposeSprite(ballSpriteP, true);
  283.     SWDisposeSprite(paddleSpriteP, true);
  284.  
  285.     SWDisposeSpriteLayer(brickLayerP);
  286.     SWDisposeSpriteLayer(ballLayerP);
  287.     SWDisposeSpriteLayer(paddleLayerP);
  288.     SWDisposeSpriteWorld(spriteWorldP);
  289.  
  290.  
  291.     //
  292.     // STEP #9: shut down the sprite world package
  293.     //
  294.  
  295.     SWExitSpriteWorld();
  296.  
  297.  
  298.     FlushEvents(everyEvent, 0);
  299.     InvalRect(&srcWindowP->portRect);
  300.     ShowCursor();
  301.     SetCursor(&qd.arrow);
  302.     SysBeep(1);
  303. }
  304.  
  305.  
  306. void BallCollideProc(SpritePtr ballSpriteP, SpritePtr brickSpriteP, Rect* sectRect)
  307. {
  308.     Rect bounceRect;
  309.     short inset;
  310.  
  311.     if (brickSpriteP->isVisible)
  312.     {
  313.         SWSetSpriteVisible(brickSpriteP, false);
  314.  
  315.         bounceRect = brickSpriteP->destFrameRect;
  316.  
  317.         inset = 4;
  318.  
  319.         bounceRect.left += inset;
  320.         bounceRect.right -= inset;
  321.  
  322.         if ((ballSpriteP->destFrameRect.top >= bounceRect.bottom) ||
  323.              (ballSpriteP->destFrameRect.bottom <= bounceRect.top) ||
  324.              (ballSpriteP->destFrameRect.left >= bounceRect.right) ||
  325.              (ballSpriteP->destFrameRect.right <= bounceRect.left))
  326.         {
  327.             ballSpriteP->horizMoveDelta = -ballSpriteP->horizMoveDelta;
  328.  
  329.             SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, 0);
  330.         }
  331.         else
  332.         {
  333.             ballSpriteP->vertMoveDelta = -ballSpriteP->vertMoveDelta;
  334.  
  335.             SWOffsetSprite(ballSpriteP, 0, ballSpriteP->vertMoveDelta);
  336.         }
  337.  
  338.     }
  339. }
  340.  
  341.  
  342. void PaddleCollideProc(SpritePtr paddleSpriteP, SpritePtr ballSpriteP, Rect* sectRect)
  343. {
  344.     short ballDelta;
  345.  
  346.         // is the paddle moving?
  347.     if (paddleSpriteP->destFrameRect.left != paddleSpriteP->oldFrameRect.left)
  348.     {
  349.             // apply some of the paddle’s movement to the ball
  350.         ballDelta = paddleSpriteP->destFrameRect.left - paddleSpriteP->oldFrameRect.left;
  351.         ballDelta /= 5;
  352.  
  353.         if (ballDelta < 5)
  354.         {
  355.             ballSpriteP->horizMoveDelta += ballDelta;
  356.         }
  357.     }
  358.  
  359.     ballSpriteP->vertMoveDelta = -ballSpriteP->vertMoveDelta;
  360.     SWOffsetSprite(ballSpriteP, 0, ballSpriteP->vertMoveDelta);
  361. }
  362.  
  363.  
  364. void PaddleMoveProc(SpritePtr paddleSpriteP, Point* spritePoint)
  365. {
  366.     Point mousePoint;
  367.  
  368.     GetMouse(&mousePoint);
  369.  
  370.     if (mousePoint.h < paddleSpriteP->moveBoundsRect.left)
  371.     {
  372.         spritePoint->h = paddleSpriteP->moveBoundsRect.left;
  373.     }
  374.     else if (mousePoint.h > paddleSpriteP->moveBoundsRect.right)
  375.     {
  376.         spritePoint->h = paddleSpriteP->moveBoundsRect.right;
  377.     }
  378.     else
  379.     {
  380.         spritePoint->h = mousePoint.h;
  381.     }
  382. }
  383.  
  384.  
  385. void BallMoveProc(
  386.     SpritePtr srcSpriteP,
  387.     Point* spritePoint)
  388. {
  389.     if (srcSpriteP->destFrameRect.left < srcSpriteP->moveBoundsRect.left)
  390.     {
  391.         srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
  392.         spritePoint->h = srcSpriteP->moveBoundsRect.left;
  393.     }
  394.     else if (srcSpriteP->destFrameRect.right > srcSpriteP->moveBoundsRect.right)
  395.     {
  396.         srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
  397.         spritePoint->h = srcSpriteP->moveBoundsRect.right -
  398.             (srcSpriteP->curFrameP->frameRect.right - srcSpriteP->curFrameP->frameRect.left);
  399.     }
  400.  
  401.     if (srcSpriteP->destFrameRect.top < srcSpriteP->moveBoundsRect.top)
  402.     {
  403.         srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  404.         spritePoint->v = srcSpriteP->moveBoundsRect.top;
  405.     }
  406.     else if (srcSpriteP->destFrameRect.top > srcSpriteP->moveBoundsRect.bottom)
  407.     {
  408.         SysBeep(1);
  409.         srcSpriteP->userData++;
  410.  
  411.         spritePoint->h = -20;
  412.         spritePoint->v = 200;
  413.  
  414.         SWSetSpriteMoveDelta(srcSpriteP, 2, 2);
  415.     }
  416. }
  417.